package ui;

import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import util.TitansResourceBundle;
import controls.TitansComposite;
import controls.TitansLabel;
import controls.TitansTitleAreaDialog;

public class UserLoginDlg extends TitansTitleAreaDialog
{

	private Text txtUsername;
	private Text txtPassword;

	private String username;
	private String password;

	public UserLoginDlg(Shell shell)
	{
		super(shell, new Point(250, 250), TitansResourceBundle
				.getString("Login_Dialog.TITLE"), TitansResourceBundle
				.getString("Login_Dialog.MESSAGE"), TitansResourceBundle
				.getImage("Login_Dialog.ICON"));
	}

	protected Control createDialogArea(Composite parent)
	{
		parent = (Composite) super.createDialogArea(parent);

		parent.setLayout(new GridLayout(1, false));
		parent.setLayoutData(new GridData(GridData.FILL_BOTH));

		TitansComposite cmpContainer = new TitansComposite(parent);
		cmpContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
		cmpContainer.setLayout(new GridLayout(2, false));

		TitansLabel lblUsername = new TitansLabel(cmpContainer, "Username");
		lblUsername.setLayoutData(new GridData());

		txtUsername = new Text(cmpContainer, SWT.BORDER);
		txtUsername.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		TitansLabel lblPassword = new TitansLabel(cmpContainer, "Password");
		lblPassword.setLayoutData(new GridData());

		txtPassword = new Text(cmpContainer, SWT.BORDER);
		txtPassword.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		return parent;
	}

	protected void okPressed()
	{
		if ("".equals(txtUsername.getText())
				|| "".equals(txtPassword.getText()))
		{
			this.setErrorMessage("Missing Data!");
			return;
		}

		username = txtUsername.getText();
		password = txtPassword.getText();

		super.close();
	}

	protected void createButtonsForButtonBar(Composite composite)
	{
		super.createButtonsForButtonBar(composite);
		this.createButton(composite, Window.OK, "OK", true);
		this.createButton(composite, Window.CANCEL, "Cancel", false);
	}

	public String getUsername()
	{
		return username;
	}

	public String getPassword()
	{
		return password;
	}

}
